package com.yonyou.common.service.messagecenter.rabbitmq.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
//import com.dianping.cat.Cat;
//import com.dianping.cat.message.Transaction;
import com.yonyou.common.service.messagecenter.rabbitmq.MessageProducer;
import com.yonyou.common.service.messagecenter.rabbitmq.MessageProducerUtil;
import com.yonyou.common.service.messagecenter.rabbitmq.entity.MessageProtocol;
import com.yonyou.common.service.messagecenter.rabbitmq.service.ResendMessageService;
import com.yonyou.common.service.messagecenter.util.MqConsts;
//import com.yonyou.gmmc.common.cat.MyContext;

import net.sf.json.JSONArray;

@Controller
@RequestMapping(value = "message")
public class MessageCenterController {
	private static Logger logger=Logger.getLogger(MessageCenterController.class);

	@Autowired
	private MessageProducer producer;
	@Autowired
	private ResendMessageService resendMessageService;

	@RequestMapping(value = "/exchange")
	public @ResponseBody String sendWithExchange(@RequestBody String body, @RequestParam String channel,
			@RequestParam String appid, @RequestParam String sign, @RequestParam String occurtime,
			@RequestParam String nonce) {

		//Transaction t = Cat.newTransaction("Service", "mqReceive");
		
		try {
//			if(proMap!=null&&!proMap.equals("")){
//				//Cat.logEvent("Service.mqReceive", "invoked");
//				Map<String,String> proMapRe = JSON.parseObject(proMap,Map.class);
//				System.out.println("proMapRe===="+proMapRe);
//				//MyContext myContext = new MyContext();
//				//myContext.setProperties(proMapRe);
//				//System.out.println("myContext===="+myContext);
//				//Cat.logRemoteCallServer(myContext);
//			}
			logger.info("--receive body:"+body);
			MessageProtocol message = new MessageProtocol();
			message.setAppid(appid);
			message.setMessagecontent(body);
			message.setSign(sign);
			message.setOccurtime(occurtime);
			message.setNonce(nonce);
			boolean flag = MessageProducerUtil.SendMessage(message, channel, "");

			logger.info("--receive done:");
			return getMessage(flag);
		} catch (Exception e) {
			//Cat.logError(e);
			//t.setStatus(e);
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
            //t.setStatus(Transaction.SUCCESS);
            //t.complete();
        }
		
		return "";
	}

	// @RequestMapping( method = RequestMethod.GET)
	// public @ResponseBody String sendWithExchange1() {
	// return getMessage1(false);
	// }
	@RequestMapping(value = "/failmessage/{level}", method = RequestMethod.GET)
	public @ResponseBody Map<String, String> resend(@PathVariable("level") String level) {
		logger.info("-- resend");
		resendMessageService.resendMessage(level);
		Map<String, String> map = new HashMap<>();
		map.put("result", "OK");
		logger.info("-- resend done");
		return map;
	}

	// @RequestMapping(value = "/send/{exchange}/{route}")
	public @ResponseBody String mqAct1(@RequestBody String body, @PathVariable("exchange") String exchange,
			@PathVariable("route") String route) {
		// boolean flag=MessageProducerUtil.SendMessage(body,exchange,route);
		// return getMessage(flag);
		return "";
	}

	private String getMessage(boolean flag) {
		Map<String, String> map = new HashMap<String, String>();
		if (flag) {
			map.put(MqConsts.RESPONSE_DATA_CODE, MqConsts.RESPONSE_DATA_VALUE_SUCCESS);
			map.put(MqConsts.RESPONSE_DATA_MESSAGE, MqConsts.RESPONSE_DATA_VALUE_SUCCESS_MESSAGE);
		} else {
			map.put(MqConsts.RESPONSE_DATA_CODE, MqConsts.RESPONSE_DATA_VALUE_INTEL_ERROR);
			map.put(MqConsts.RESPONSE_DATA_MESSAGE, MqConsts.RESPONSE_DATA_VALUE_INTEL_ERROR_MESSAGE);
		}
		JSONArray jarr = JSONArray.fromObject(map);
		return jarr.toString();
	}

	private String getMessage1(boolean flag) {
		Map<String, String> map = new HashMap<String, String>();
		if (flag) {
		} else {
			map.put(MqConsts.RESPONSE_DATA_CODE, MqConsts.RESPONSE_DATA_VALUE_NON_BODY_ERROR);
			map.put(MqConsts.RESPONSE_DATA_MESSAGE, MqConsts.RESPONSE_DATA_VALUE_NON_BODY_ERROR_MESSAGE);
		}
		JSONArray jarr = JSONArray.fromObject(map);
		return jarr.toString();
	}
}
